# 共用的自定义工具类
import functools

from flask import session, current_app, g

from info.models import User


def do_index_class(index):
    """返回指定索引对应的类名"""

    if index == 0:
        return "first"
    elif index == 1:
        return "second"
    elif index == 2:
        return "third"
    return ""


def user_login_data(f):

    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        # 查询用户登录信息
        # 获取到当前登录用户的id
        user_id = session.get('user_id', None)

        # 通过ID获取用户信息
        user = None
        if user_id:
            try:
                user = User.query.get(user_id)
                print(user)

            except Exception as e:
                current_app.logger.error(e)

        # 把查询出来的数据赋值给g变量
        g.user = user

        return f(*args, **kwargs)
    return wrapper




# def query_user_data():
#     # 查询用户登录信息
#     # 获取到当前登录用户的id
#     user_id = session.get('user_id', None)
#
#     # 通过ID获取用户信息
#     user = None
#     if user_id:
#         try:
#             user = User.query.get(user_id)
#
#         except Exception as e:
#             current_app.logger.error(e)
#
#         return user
#     return None
